package com.inspur.gs.sbssdl.dzyhpgl.dzyhplygl.dzyhplygl.front.sbssyhplyqrdetail_frm.voactions;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.parameter.retrieve.RespectiveRetrieveResult;
import com.inspur.edp.bff.spi.action.modify.AfterModifyAction;
import com.inspur.edp.bff.api.manager.context.ModifyContext;
import com.inspur.edp.cef.api.VoidReturnType;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;

import java.math.BigDecimal;
import java.util.Collection;

public class ChangeAfterVOAction extends AfterModifyAction {

	public ChangeAfterVOAction(ModifyContext context) {
		super(context);
	}
	@Override
	public void execute() {
		IStandardLcp lcp = getLcp();

		Collection<String> dataIds = lcp.getDataIds();

		dataIds.forEach(i -> {
			executeAfter(lcp, i);
		});
	}

	public void executeAfter(IStandardLcp lcp, String id) {
        RespectiveRetrieveResult retrieve = lcp.retrieve(id);
        IEntityData data = retrieve.getData();

        IEntityDataCollection yhpmx = data.getChilds().get("yhpmx");
        if (yhpmx == null) {
            return;
        }

        BigDecimal zj = yhpmx.stream()
                .reduce(new BigDecimal(0),
                        (a, b) -> a.add(new BigDecimal(b.getValue("zj").toString())),
                        BigDecimal::add);

        ModifyChangeDetail change = new ModifyChangeDetail(id);

        change.getPropertyChanges().put("lyzj", zj);

        lcp.modify(change);
        lcp.save();
	}
}
